﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SQLHelper;

public partial class admin_User_Search : System.Web.UI.Page
{
    string search = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.OnlyAdmin();

        if (string.IsNullOrEmpty(Request.QueryString["search"]))
        {
            Response.Redirect("~/admin/User/default.aspx" + search + "");
        }
        else
        {
            search = Request["search"];
            bind();
        }
    }

    protected void Search(object sender, EventArgs e)
    {
        string search = TextBox1.Text.ToString();
        if (search != null)
        {
            Response.Redirect("~/admin/User/Search.aspx?search=" + search + "");
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        bind();
    }

    void bind()
    {
        string sql = "select u.id,u.name,u.email,u.created_at,r.role_id,rr.display_name from users u, role_user r,roles rr where u.id = r.user_id and r.role_id = rr.id and u.name like '%" + search + "%'";
        DataSet res = DB.GetDataWithQuerySecond(sql, "users");
        GridView1.DataSource = res;
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
        DB.ColseSqlCon();
    }
}